package org.swingplus.core.utils;

import org.swingplus.core.bean.DefinitionMetadata;
import org.swingplus.core.bean.RootBeanDefinition;
import org.swingplus.core.constant.bean.DependentOn;
import org.swingplus.core.factory.AbstractBeanDefinition;
import org.swingplus.core.factory.MutablePropertyValues;
import org.swingplus.core.factory.config.BeanDefinition;
import org.swingplus.core.util.AnnoManageUtil;

import javax.annotation.Nullable;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import static org.swingplus.core.utils.ClassFileInfoParseUtil.getAutowiredByBeanName;
import static org.swingplus.core.utils.ClassFileInfoParseUtil.getAutowiredFields;

public class ClassFileInfoParse {
    private Class clazz;
    private Object instance;

    public ClassFileInfoParse(Class clazz) {
        this.clazz = clazz;
    }


    public ClassFileInfoParse(Object instance) {
        this.instance = instance;
        this.clazz = instance.getClass();
    }

//    @Nullable
//    public DefinitionMetadata getDefinitionMetadata(){
//        return  ClassFileInfoParseUtil.getDefinitionMetadata(instance);
//    }

    @Nullable
    public DefinitionMetadata getDefinitionMetadata() {
        return ClassFileInfoParseUtil.getDefinitionMetadata(this.clazz, null);
    }


    /**
     * 获取类的基本信息
     *
     * @param clazz
     * @return
     */
    public static BeanDefinition parse(Class clazz) {
        RootBeanDefinition definition = new RootBeanDefinition();
        definition.setBeanClass(clazz);
        Field[] fields = clazz.getFields();
        Field[] declaredFields = clazz.getDeclaredFields();
        definition.setDeclaredFields(declaredFields);
        String[] autowiredByBeanName = getAutowiredByBeanName(declaredFields);
//        definition.setAutowiredField(autowiredFields);
        definition.setDependsOn(autowiredByBeanName);
        if(autowiredByBeanName.length>0){
            definition.setExistsAutowired(true);
        }else{
            definition.setExistsAutowired(false);
        }
        Method[] methods = clazz.getMethods();
        definition.setMethods(methods);
        definition.setMethodSize(methods.length);

        boolean flag = AnnoManageUtil.existAutowiredConstructors(clazz);
        if (flag) {
            definition.setDependentOn(DependentOn.CONSTRUCTOR.toString());
        } else {
            definition.setDependentOn(DependentOn.SETTTER.toString());
        }
        return definition;

    }










}
